/**
 * @author whailong  [wanghl@ncsi.com.cn]
 * @date 16 Jun 2014 10:49:58
 */
package com.surent.controller.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ListView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.surent.controller.activity.AtyPublishHouse;
import com.surent.model.dbmodel.House;
import com.surent.model.viewmodel.HouseList;
import com.surent.utils.HouseService;
import com.surent.view.adapter.AdapterHouseList;
import com.surent.view.custom.ExpandTabView;
import com.surent.view.custom.ViewArea;
import com.surent.view.custom.ViewPrice;
import com.surent.view.custom.ViewSortBy;

/**
 * 
 * @author whailong [wanghl@ncsi.com.cn]
 * @date 16 Jun 2014 10:49:58
 */
public class FragHouse extends Fragment implements OnRefreshListener {

	private Activity atyParent;

	@ViewInject(R.id.refresh_layout_houseList)
	private PullToRefreshLayout ptrLayout;
	@ViewInject(R.id.lv_house)
	private ListView lvHouse;
	@ViewInject(R.id.expandview_house)
	private ExpandTabView filterView;

	private ViewArea viewArea;
	private ViewPrice viewPrice;
	private ViewSortBy viewSortBy;

	private ArrayList<HouseList> listHouse;
	private AdapterHouseList adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.setHasOptionsMenu(true);
		return inflater.inflate(R.layout.frag_house, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		atyParent = getActivity();
		ViewUtils.inject(this, atyParent);

		ActionBarPullToRefresh.from(atyParent).allChildrenArePullable().listener(this).setup(ptrLayout);
		adapter = new AdapterHouseList(atyParent);

		lvHouse.setAdapter(adapter);
		refreshData();
		viewArea = new ViewArea(atyParent);
		viewPrice = new ViewPrice(atyParent);
		viewSortBy = new ViewSortBy(atyParent);

		ArrayList<String> mTextArray = new ArrayList<String>();
		mTextArray.add("全部区域");
		mTextArray.add("价格不限");
		mTextArray.add("默认排序");
		ArrayList<View> mViewArray = new ArrayList<View>();
		mViewArray.add(viewArea);
		mViewArray.add(viewPrice);
		mViewArray.add(viewSortBy);
		filterView.setValue(mTextArray, mViewArray);

		lvHouse.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

				if (lvHouse.getLastVisiblePosition() != lvHouse.getCount() - 1)
					return;

				switch (scrollState) {

				// TODO 设置当前网络有Wifi时自动加载
				// case 空闲时
				case OnScrollListener.SCROLL_STATE_IDLE:
					ptrLayout.setRefreshing(true);// TODO handle
					ArrayList<HouseList> listTemp = new ArrayList<HouseList>();
					HouseList item;
					for (int i = 0; i < 5; i++) {
						// item = new ChumList("189-" + i, "小王-" + i, "男",
						// "http://img0.bdstatic.com/img/image/shouye/mnwlmn-11642508183.jpg",
						// "wantDescribe",
						// "海德", 100 * i, "工业园区");
						// listTemp.add(item);
					}

					ptrLayout.setRefreshing(false);// TODO handle
					adapter.addAll(listTemp);
					break;
				// case 还没松手时
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					break;
				default:
					break;
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			}
		});
	}

	@Override
	public void onRefreshStarted(View view) {
		// ptrLayout.setRefreshing(true);
		refreshData();
	}

	/**
	 * @CreateTime Oct 31, 2014 4:15:59 PM
	 */
	private void refreshData() {
		AVQuery<House> query = House.getQuery(House.class);
		query.limit(10);
		query.findInBackground(new FindCallback<House>() {

			@Override
			public void done(List<House> list, AVException e) {
				if (e == null) {
					HouseService hs = new HouseService();
					List<HouseList> houseList = new ArrayList<HouseList>();

					for (int i = 0; i < list.size(); i++) {
						houseList.add(hs.toHouseList(list.get(i)));
					}

					adapter.clear();
					adapter.addAll(houseList);
					adapter.notifyDataSetChanged();
					ptrLayout.setRefreshComplete();
				}
			}
		});
	}

	@OnItemClick(R.id.lv_house)
	void onLvChumItemOnClick(AdapterView<?> parent, View view, int position, long id) {
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.frag_house, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// startActivity(new Intent(atyParent, AtyIndexListview.class));
		startActivity(new Intent(atyParent, AtyPublishHouse.class));

		return super.onOptionsItemSelected(item);
	}
}
